library(haven)
library(tidyverse)

ces <- read_dta("Race and Congress paper/dataverse_files/HillHuberSurveyMeasure_ReplicationArchive/CCES2014/RawData/HillHuberCCES2014.dta")
lucid <- read_dta("Race and Congress paper/dataverse_files/HillHuberSurveyMeasure_ReplicationArchive/Lucid/RawData/HillHuberLucidwide_withweights.dta")

ces <- ces %>%
  mutate(
    race_cat = case_when(
      d_race_black == 1 ~ "Black",
      d_race_hispanic == 1 ~ "Hispanic",
      d_race_other == 1 ~ "Other",
      d_race_black == 0 & d_race_hispanic == 0 & d_race_other == 0 ~ "White",
      TRUE ~ NA_character_
    ))

ces_grp <- ces %>%
  group_by(race_cat, treatment, bill) %>%
  summarise(vote_m = weighted.mean(vote_yea, weight, na.rm = T)) %>%
  filter(!is.na(bill)) %>%
  pivot_wider(names_from = race_cat, values_from = vote_m) %>%
  mutate(WB_split = abs(White-Black),
         WH_split = abs(White-Hispanic)) %>%
  group_by(treatment) %>%
  summarise(av_WB_split = mean(WB_split),
            av_WH_split = mean(WH_split))

lucid <- lucid %>%
  mutate(
    race_cat = case_when(
      dem_raceblack == 1 ~ "Black",
      dem_hispanic == 1 ~ "Hispanic",
      dem_raceother == 1 ~ "Other",
      dem_raceblack == 0 & dem_hispanic == 0 & dem_raceother == 0 ~ "White",
      TRUE ~ NA_character_
    )) %>%
    pivot_longer(
      cols = starts_with("voterc_blk"),
      names_to = "billid",
      values_to = "vote"
    ) %>%
    mutate(
      block = case_when(
        str_detect(billid, "blk1") ~ 1,
        str_detect(billid, "blk2") ~ 2,
        TRUE ~ NA_real_
      )
    ) %>%
    mutate(
      assignedcondition = ifelse(block== 1, lucid$block1_treat, lucid$block2_treat)
    )


lucid_grp <- lucid %>%
  group_by(race_cat, assignedcondition, billid) %>%
  summarise(vote_m = weighted.mean(vote, weight, na.rm = T)) %>%
  pivot_wider(names_from = race_cat, values_from = vote_m) %>%
  mutate(WB_split = abs(White-Black),
         WH_split = abs(White-Hispanic)) %>%
  group_by(assignedcondition) %>%
  summarise(av_WB_split = mean(WB_split),
            av_WH_split = mean(WH_split))

lucid_grp <- lucid %>%
  group_by(race_cat, assignedcondition, billid) %>%
  summarise(vote_m = mean(vote, na.rm = T)) %>%
  pivot_wider(names_from = race_cat, values_from = vote_m) %>%
  mutate(WB_split = abs(White-Black),
         WH_split = abs(White-Hispanic))

write.csv(lucid_grp, "HHLuciddata_racebillagg.csv")
